package cn.ekuma.epos.datalogic.define.dao.crm;

import java.util.Date;
import java.util.List;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.crm.I_CustomerStoreValueCard;

import com.openbravo.bean.Customer;
import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class CustomerStoreValueCardDAO extends ModifiedLogDAO<CustomerStoreValueCard>{

    public CustomerStoreValueCardDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                    "STOREVALUECARD",
                    new Field[]{
                       new Field(I_CustomerStoreValueCard.ID,Datas.STRING, Formats.STRING),
                       new Field(I_CustomerStoreValueCard.CUSTOMER_ID,Datas.STRING, Formats.STRING,Customer.class),
                       new Field(I_CustomerStoreValueCard.CREATEDATE,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_CustomerStoreValueCard.STATE,Datas.INT, Formats.INT),
                       new Field(I_CustomerStoreValueCard.VALIDITYDATE,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_CustomerStoreValueCard.CURLIMIT,Datas.DOUBLE, Formats.DOUBLE,true),
                       new Field(I_CustomerStoreValueCard.BACKLIMIT,Datas.DOUBLE, Formats.DOUBLE),
                       new Field(I_CustomerStoreValueCard.MEMO,Datas.STRING, Formats.STRING),
                       new Field(I_CustomerStoreValueCard.PASSWORD,Datas.STRING, Formats.STRING),
                       new Field(I_CustomerStoreValueCard.TOTALRECHARGE,Datas.DOUBLE, Formats.DOUBLE,true),
                       new Field(I_CustomerStoreValueCard.TOTALCONSUMER,Datas.DOUBLE, Formats.DOUBLE,true),
                       new Field(I_CustomerStoreValueCard.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_CustomerStoreValueCard.CARDNUM,Datas.STRING, Formats.STRING)
                     },
                    new int[]{0}
                    );
    }

	@Override
	public void writeInsertValues(DataWrite dp, CustomerStoreValueCard obj)
			throws BasicException {
	      dp.setString(1,obj.getId());
	      dp.setString(2,obj.getCustomerId());
	      dp.setTimestamp(3,obj.getCreateDate());
	      dp.setInt(4,obj.getState());
	      dp.setTimestamp(5,obj.getValidityDate());
	      dp.setDouble(6,obj.getCurLimit());
	      dp.setDouble(7,obj.getBackLimit());
	      dp.setString(8,obj.getMemo());
	      dp.setString(9,obj.getPassword());
	      dp.setDouble(10,obj.getTotalRecharge());
	      dp.setDouble(11,obj.getTotalConsumer());
	      dp.setTimestamp(12,obj.getLastModified());
	      dp.setString(13,obj.getCardNum());
		
	}
	
	
	

	@Override
	public void writeUpdateRealValues(DataWrite dp, CustomerStoreValueCard obj)
			throws BasicException {
		  dp.setString(1,obj.getId());
	      dp.setString(2,obj.getCustomerId());
	      dp.setTimestamp(3,obj.getCreateDate());
	      dp.setInt(4,obj.getState());
	      dp.setTimestamp(5,obj.getValidityDate());
	      //dp.setDouble(6,obj.getCurLimit());
	      dp.setDouble(6,obj.getBackLimit());
	      dp.setString(7,obj.getMemo());
	      dp.setString(8,obj.getPassword());
	      //dp.setDouble(10,obj.getTotalRecharge());
	      //dp.setDouble(11,obj.getTotalConsumer());
	      dp.setTimestamp(9,obj.getLastModified());
	      dp.setString(10,obj.getCardNum());
	}

	@Override
	public Class getSuportClass() {
		return CustomerStoreValueCard.class;
	}

	@Override
	public CustomerStoreValueCard readValues(DataRead dr,
			CustomerStoreValueCard obj) throws BasicException {
		if(obj==null)
			obj=new CustomerStoreValueCard();
		obj.setId(dr.getString(1));
		obj.setCustomerId(dr.getString(2));
		obj.setCreateDate(dr.getTimestamp(3));
		obj.setState(dr.getInt(4));
		obj.setValidityDate(dr.getTimestamp(5));
		obj.setCurLimit(dr.getDouble(6));
		obj.setBackLimit(dr.getDouble(7));
		obj.setMemo(dr.getString(8));
		obj.setPassword(dr.getString(9));
		obj.setTotalRecharge(dr.getDouble(10));
		obj.setTotalConsumer(dr.getDouble(11));
		obj.setLastModified(dr.getTimestamp(12));
		obj.setCardNum(dr.getString(13));
		return obj;
	}
	
	@Override
	public List<CustomerStoreValueCard> list(Object filter) throws BasicException {
		
		return new StaticSentence(s, new QBFBuilder(
                this.getTableDefinition().getListSQL()+" WHERE ?(QBF_FILTER) "
                , new String[]{I_CustomerStoreValueCard.CREATEDATE, I_CustomerStoreValueCard.CREATEDATE, I_CustomerStoreValueCard.CUSTOMER_ID}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING}), this).list(filter);
	}
	
	
	public CustomerStoreValueCard findStoreValueCardByNum(String cardNum)throws BasicException{
		return (CustomerStoreValueCard) new PreparedSentence(s, getTableDefinition().getListSQL()+" WHERE CARDNUM = ? ", SerializerWriteString.INSTANCE, this).find(cardNum);
	}
	
	
	public void storeValueCard_In(final String id,final double value) throws BasicException{
		new PreparedSentence(
				s,
				"UPDATE STOREVALUECARD SET CURLIMIT = ( CURLIMIT+? ),LASTMODIFIED=? WHERE ID = ? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				this.setDouble(1, value);
				setTimestamp(2,new Date());
				setString(3, id);
			}
		});
	}
	
	public void storeValueCard_Out(final String id,final double value) throws BasicException{
		new PreparedSentence(
				s,
				"UPDATE STOREVALUECARD SET CURLIMIT = ( CURLIMIT-? ),LASTMODIFIED=? WHERE ID = ? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				this.setDouble(1, value);
				setTimestamp(2,new Date());
				setString(3, id);
			}
		});
	}
	
	public void rechargeValueCard(final String id,final double value) throws BasicException{
		new PreparedSentence(
				s,
				"UPDATE STOREVALUECARD SET CURLIMIT = ( CURLIMIT+? ),TOTALRECHARGE=(TOTALRECHARGE+?),LASTMODIFIED=? WHERE ID = ? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				setDouble(1, value);
				setDouble(2, value);
				setTimestamp(3,new Date());
				setString(4, id);
			}
		});
	}
	
	public void consumerValueCard(final String id,final double value) throws BasicException{
		new PreparedSentence(
				s,
				"UPDATE STOREVALUECARD SET CURLIMIT = ( CURLIMIT-? ),TOTALCONSUMER=(TOTALCONSUMER+?),LASTMODIFIED=? WHERE ID = ? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				setDouble(1, value);
				setDouble(2, value);
				setTimestamp(3,new Date());
				setString(4, id);
			}
		});
	}
	
	
	public void changeCardState(final String id,final int state)throws BasicException{
		new PreparedSentence(
				s,
				"UPDATE STOREVALUECARD SET STATE = ?,LASTMODIFIED=? WHERE ID = ? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				setInt(1, state);
				setTimestamp(2,new Date());
				setString(3, id);
			}
		});
	}
}
